﻿using System;

using System.Drawing;
using System.Text;
using System.Windows.Forms;
using csClasses;

namespace GUI
{
    public partial class  Form1 : Form
    {
        private System.Windows.Forms.Button[] _buttonArray;
        private bool _isX;
        private bool _isGameOver;
        public char[][] board;
        private Score score;
        private CheckForWin cfw;

        public Form1()
        {
            InitializeComponent();
        }

      

		private void Form1_Load(object sender, System.EventArgs e)
		{
			_buttonArray = new Button[9] {button1, button2, button3, button4, button5, button6, button7, button8, button9};
			for(int i = 0; i < 9; i++)
				this._buttonArray[i].Click += new System.EventHandler(this.ClickHandler);

			InitTicTacToe();  // set the defaults
		}

		private void butPlay_Click(object sender, System.EventArgs e)
		{
			InitTicTacToe();	
		}

		private void InitTicTacToe()
		{
			for(int i=0;i<9;i++)
			{
				_buttonArray[i].Text = "";
				_buttonArray[i].ForeColor = Color.Black;
				_buttonArray[i].BackColor = Color.LightGray;
				_buttonArray[i].Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			}
			this._isX = true;
			this._isGameOver = false;
            score = new Score();
            board = Game.upphafsStillaLeik(board);
            cfw = new CheckForWin();
		}

        private void ResetGame() {
            button1.Text = "";
            button2.Text = "";
            button3.Text = "";
            button4.Text = "";
            button5.Text = "";
            button6.Text = "";
            button7.Text = "";
            button8.Text = "";
            button9.Text = "";
            _isGameOver = false;
            _isX = true;
            return;
        }

		private void ClickHandler(object sender, System.EventArgs e)
		{
			Button tempButton = (Button)sender;

			if( tempButton.Text != "" )	// ef takki er ekki tómur
			{
				MessageBox.Show("Buið er að merkja í þennan reit!","ATH.",MessageBoxButtons.OK );
				return;
			}
            board = SetBoardFromButton(tempButton.Name,board);
            if (_isX)	// put the character in the Text property
            {
                tempButton.Text = "X";
                label1.Text = "Leikmaður O á að gera";
                int win = cfw.WhoWon(board);
                if (win == 1)
                {
                    score.increasePlayer1Score();
                    board = Game.upphafsStillaLeik(board);
                    _isGameOver = true;
                }
                if (win == 2)
                {
                    score.increaseTie();
                    board = Game.upphafsStillaLeik(board);
                    _isGameOver = true;
                }
            }
            else
            {
                tempButton.Text = "O";
                label1.Text = "Leikmaður X á að gera";
                int win = cfw.WhoWon(board);
                if (win == 1)
                {
                    score.increasePlayer2Score();
                    board = Game.upphafsStillaLeik(board);
                    _isGameOver = true;
                }
                if (win == 2)
                {
                    score.increaseTie();
                    board = Game.upphafsStillaLeik(board);
                    _isGameOver = true;
                }
            }
            if (this._isGameOver)
            {
                string messdisplay;
                if (score.getTie() == 1)
                {
                    messdisplay = "Jafntefli";
                }
                else
                {
                    messdisplay ="Leikmaður "+ tempButton.Text + " vann";
                }

                MessageBox.Show(messdisplay + "\n"
                               + "Leikmaður X: " + score.getPlayer1().ToString() + "\n"
                               + "Leikmaður O: " + score.getPlayer2().ToString() + "\n"
                               + "Jafntefli: " + score.getTie() + "\n", "Leik lokið ", MessageBoxButtons.OK);
                ResetGame();
            }
            else //Else clause added as when resetting the game O would begin the next game, regardless of winner.
            {
                _isX = !_isX;	// prepare for next character
            }
		}
        private char[][] SetBoardFromButton(String buttonName, char[][] board2)
        {
            int ButtonNumber = int.Parse(buttonName.Substring(6, 1));
            int counter = 0;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    counter++;
                    if (counter == ButtonNumber)
                    {
                        if (_isX)
                            board2[i][j] = 'x';
                        else
                            board2[i][j] = 'o';
                    }
                }
            }
            return board2;
        }

        private void butPlay_Click_1(object sender, EventArgs e)
        {
            board = Game.upphafsStillaLeik(board);
            ResetGame();
        }
	}


    }

